layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //用户列表
    var tableIns = table.render({
        elem: '#userList',
      //  url : '../../json/userList.json',
        url : '../../../logistics/page',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 15,
        id : "userListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'projectName', title: '项目名称', minWidth:100, align:"center"},
            {field: 'buildingName', title: '栋号', minWidth:10, align:"center"},
            {field: 'floors', title: '楼层', minWidth:100, align:"center"},
        //    {field: 'towerCrane', title: '塔吊', minWidth:100, align:"center"},
            {field: 'projectLeader', title: '项目负责人', minWidth:100, align:"center"},
            {field: 'driverName', title: '司机', minWidth:100, align:"center"},
            {field: 'trainNo', title: '车次', minWidth:100, align:"center"},
            {field: 'productName', title: '构件', minWidth:100, align:"center"},
            {field: 'setTime', title: '规定时间', minWidth:100, align:"center"},
            {field: 'actualTime', title: '送达时间', minWidth:100, align:"center"},

         //   {field: 'remark', title: '工单备注', minWidth:100, align:"center"},
            {field: 'finishState', title: '物流状态', minWidth:100, align:"center",
                templet: function(d){
                //工单状态： 0 待审核 1,通过 2.驳回3.修改
                    if(d.finishState == '0'){
                        return '未开始'
                    }
                    if(d.finishState == '1'){
                        return '正常送达'
                    }
                    if(d.finishState == '2'){
                        return '延迟送达'
                    }
                    if(d.finishState == '3'){
                        return '未完成'
                    }
                    return ''
                }
            },
            {field: 'finishCause', title: '延迟原因', minWidth:100, align:"center"},
            {title: '操作', minWidth:175, templet:'#userListBar',fixed:"right",align:"center"}
        ]]
    });

    /**
     *  u.user_name applyUser,uuu.user_name auditUser,
     p.`name` projectName,p.building,
     w.id workOrderId,w.`name` orderName,w.floor ,w.tower_crane towerCrane,w.remark,w.state,w.order_type orderType
     */
    var projectName = "";
    var state;

    //搜索 功能
    $(".search_btn").on("click", function () {
        table.reload("userListTable", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
               // projectName: $("#searchName").val().trim(),  //搜索的 关键字  姓名,手机号,账号
                finishState : $("#orderState").val()
            }
        })

    });

    //添加
    function addUser(edit){
        var index = layui.layer.open({
            title : "工单详情",
            type : 2,
            content : "workorderInfo.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){

                    /**
                     * 项目名称
                     栋号
                     楼层
                     塔吊
                     工单申请人
                     工单审核人
                     工单状态
                     */
                    body.find("#workOrderId").val(edit.workOrderId);  //设备名称
                    // body.find("#facilityType").val(edit.type);  //设备名称

                    body.find("#projectName").val(edit.projectName);  //
                    body.find("#buildingName").val(edit.buildingName);  //
                    body.find("#floor").val(edit.floor);  //
                    body.find("#towerCrane").val(edit.towerCrane);  //
                    body.find("#remark").val(edit.remark);  //
                    body.find("#startTime").val(edit.startTime);  //
                    body.find("#endTime").val(edit.endTime);  //
                    body.find("#applyUser").val(edit.applyUser);  //
                    body.find("#auditUser").val(edit.auditUser);  //
                    body.find("#auditOpinion").val(edit.auditOpinion);  // confirmTime
                    body.find("#confirmTime").val(edit.confirmTime);  // confirmTime
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        window.sessionStorage.setItem("index",index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }
    $(".addNews_btn").click(function(){
        addUser();
    })

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('userListTable'),
            data = checkStatus.data,
            newsId = [];
        if(data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].newsId);
            }
            layer.confirm('确定删除选中的数据？', {icon: 3, title: '提示信息'}, function (index) {
                $.post("../../workorder/del",{
                    id : data.id  //将需要删除的newsId作为参数传入
                },function(data){
                    tableIns.reload();
                    layer.close(index);
                })
            })
        }else{
            layer.msg("请选择需要删除的数据");
        }
    })

    //列表操作
    table.on('tool(userList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addUser(data);
        }else   if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此数据？',{icon:3, title:'提示信息'},function(index){
                $.post("../../logistics/delete",{
                    id : data.logisticsId  //将需要删除的newsId作为参数传入
                },function(data){
                    top.layer.msg(data.msg);
                    tableIns.reload();
                    layer.close(index);
                })
            });
        }
    });

})
